package com.lakeworks.service.impl;

import com.lakeworks.common.core.text.Convert;
import com.lakeworks.domain.SysDefective;
import com.lakeworks.mapper.SysDefectiveMapper;
import com.lakeworks.service.ISysDefectiveService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysDefectiveServiceImpl implements ISysDefectiveService {

    @Resource
    private SysDefectiveMapper sysDefectiveMapper;

    @Override
    @Cacheable(value = "defective", key = "#id",unless = "#result == null")
    public SysDefective defectiveById(Long id) {
        return sysDefectiveMapper.selectSysDefectiveById(id);
    }

    @Override
    @Cacheable(value = "sysDefectives", key = "#root.methodName",unless = "#result?.size() == 0")
    public List<SysDefective> defectiveList() {
        return sysDefectiveMapper.selectSysDefectiveList(new SysDefective());
    }

    @Override
    @CacheEvict(value = "sysDefectives", key = "'defectiveList'")
    public int insertSysDefective(SysDefective sysDefective) {
        return sysDefectiveMapper.insertSysDefective(sysDefective);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "sysDefectives", key = "'defectiveList'"),
            @CacheEvict(value = "defective", key = "#sysDefective.id")
    })
    public int updateSysDefective(SysDefective sysDefective) {
        return sysDefectiveMapper.updateSysDefective(sysDefective);
    }

    @Override
    @CacheEvict(value = "sysDefectives", key = "'defectiveList'")
    public int deleteSysDefectiveByIds(String ids) {
        return sysDefectiveMapper.deleteSysDefectiveByIds(Convert.toStrArray(ids));
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "sysDefectives", key = "'defectiveList'"),
            @CacheEvict(value = "defective", key = "#id")
    })
    public int deleteSysDefectiveById(Long id) {
        return sysDefectiveMapper.deleteSysDefectiveById(id);
    }

    @Override
    public List<SysDefective> selectSysRiskList() {
        return sysDefectiveMapper.selectSysRiskList();
    }
}
